package org.bigjava.travel.controller;


import org.bigjava.travel.bean.User;
import org.bigjava.travel.service.UserService;
import org.bigjava.travel.utils.PageParam;
import org.bigjava.travel.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class UserController extends BaseController {

        @Autowired
        UserService userService;

        @RequestMapping("/userList")
        public ModelAndView userList(PageParam pageParam, @RequestParam(value = "query", required = false) String query) throws Exception {
            ModelAndView mv = this.getModeAndView();
            if(pageParam.getPageNumber()<1){
                pageParam =new PageParam();
                long count = 0;
                try {
                    count = userService.count();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                pageParam.setCount(count);
                if(count<=10){
                    pageParam.setSize(1);
                }else{
                    pageParam.setSize(count%10==0?count/10:count/10+1);
                }
                pageParam.setPageNumber(1);
                pageParam.setPageSize(10);
            }
            List<User> list = userService.findByPage(pageParam.getPageNumber(),pageParam.getPageSize(), query);
            mv.addObject("pageData", list);
            if (Tools.notEmpty(query)) {
                mv.addObject("query", query);
                pageParam.setCount(list.size());
                if (list.size() > pageParam.getPageSize()) {
                    pageParam.setSize(list.size() / pageParam.getPageSize());
                } else {
                    pageParam.setSize(1);
                }
            }
            mv.addObject("pageParam",pageParam);
            mv.setViewName("user/allUsers");
            return mv;
        }


        @RequestMapping("/userAdd")
        public ModelAndView userAdd(){
            ModelAndView mv = this.getModeAndView();
            mv.addObject("user",new User());
            mv.setViewName("user/userEdit");
            return mv;
        }

        @RequestMapping("/userView")
        public ModelAndView userView(Integer id){
            ModelAndView mv = this.getModeAndView();
            try {
                mv.addObject("user",userService.findById(id));
            }catch (Exception e){
                e.printStackTrace();
            }
            mv.setViewName("user/userView");
            return mv;
        }

        @RequestMapping("/userEdit")
        public ModelAndView userEdit(Integer id){
            ModelAndView mv = this.getModeAndView();
            try {
                mv.addObject("user",userService.findById(id));
            }catch (Exception e){
                e.printStackTrace();
            }
            mv.setViewName("user/userEdit");
            return mv;
        }

        @RequestMapping("/userSave")
        public ModelAndView userSave(HttpServletRequest request, Integer id, RedirectAttributes redirectAttributes) throws Exception {
            ModelAndView mv = this.getModeAndView();
            User user = null;
            try {
                if(Tools.notEmpty(id)){
                    user = userService.findById(id);
                }else{
                    user = new User();
                }
                this.bindValidateRequestEntity(request,user);
                if (Tools.isEmpty(user.getId())){
                    User object = userService.findByUserName(user.getUserName());
                    if (object != null) {
                        mv.addObject("message","用户名已存在!");
                        mv.addObject("user",user);
                        mv.setViewName("user/userEdit");
                        return mv;
                    }
                    userService.save(user);
                }else{
                    userService.update(user);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            mv.addObject("pageData", userService.findByPage(1, 10,null));
            PageParam pageParam =new PageParam();
            long count = 0;
            try {
                count = userService.count();
            } catch (Exception e) {
                e.printStackTrace();
            }
            pageParam.setCount(count);
            if(count<=10){
                pageParam.setSize(1);
            }else{
                pageParam.setSize(count%10==0?count/10:count/10+1);
            }
            pageParam.setPageNumber(1);
            pageParam.setPageSize(10);
            mv.addObject("pageParam",pageParam);
            mv.setViewName("user/allUsers");
            return mv;
        }

        @RequestMapping("/userDelete")
        public String userDelete(Integer id){
            if(Tools.notEmpty(id)){
                try {
                    userService.deleteById(id);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            return REDIRECT+"/admin/userList";
        }
}
